﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using ProgNetComponents;
using ProgNetComponents.Design.Attributes;
using DevExpress.Utils.Editors;
using System.Drawing.Design;
using DevExpress.XtraEditors.Mask;
using System.Data;
using ProgNetComponents.NamedObjects;

namespace ProgNetDxComponents.ExtenderProviders.Browsers
{
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class BrowserColumn : ProgNetComponents.Components.CustomTypeDescriptorObject, ICloneable, INamedObject
    {
        public BrowserColumn()
        {
            Visible = true;
            SearchField = false;
            Width = 50;
            ColumnType = BrowserColumnType.TextColumn;
            FilterCondition = FilterType.BeginsWith;
        }

        #region Fields
        private BrowserColumnType _ColumnType;
        private MaskProperties _Mask = new MaskProperties();
        private string _BrowserName;
        #endregion

        #region Properties
        [Category(Categories.Display)]
        public string Caption
        {
            get;
            set;
        }

        [Category(Categories.Display)]
        public string Name
        {
            get;
            set;
        }

        [Category(Categories.Display)]
        public bool Visible
        {
            get;
            set;
        }

        [Category(Categories.Display),
        RefreshProperties(System.ComponentModel.RefreshProperties.All)]
        public bool SearchField
        {
            get;
            set;
        }

        [Category(Categories.Data),
        DynamicBrowsableByProperty("SearchField", true)]
        public FilterType FilterCondition
        {
            get;
            set;
        }

        [Category(Categories.Display)]
        public int Width
        {
            get;
            set;
        }

        [RefreshProperties(System.ComponentModel.RefreshProperties.All),
        Category(Categories.Display)]
        public BrowserColumnType ColumnType
        {
            get { return _ColumnType; }
            set
            {
                _ColumnType = value;
                if (!this.IsAppRunning())
                {
                    if (value == BrowserColumnType.CheckColumn)
                    {
                        if (ValueChecked == null)
                            ValueChecked = true;
                        if (ValueUnchecked == null)
                            ValueUnchecked = false;
                        DefaultValue = ValueUnchecked;
                    }
                    else
                    {
                        DefaultValue = null;
                    }
                }
                
            }
        }

        [Category(Categories.Data)]
        public string Expression
        {
            get;
            set;
        }

        [Category(Categories.Data),
        Editor(typeof(UIObjectEditor), typeof(UITypeEditor))]
        public object DefaultValue { get; set; }

        #region CheckColumn
        [Category(Categories.Data),
        DynamicBrowsableByProperty("ColumnType", BrowserColumnType.CheckColumn),
        Editor(typeof(UIObjectEditor), typeof(UITypeEditor))]
        public object ValueChecked
        {
            get;
            set;
        }

        [Category(Categories.Data),
        DynamicBrowsableByProperty("ColumnType", BrowserColumnType.CheckColumn),
        Editor(typeof(UIObjectEditor), typeof(UITypeEditor))]
        public object ValueUnchecked
        {
            get;
            set;
        }
        
        [Category(Categories.Data),
        DynamicBrowsableByProperty("ColumnType", BrowserColumnType.CheckColumn),
        DynamicBrowsableByProperty("SearchField", true)]
        public bool AllowGrayed
        {
            get;
            set;
        }
        #endregion

        #region TextColumn
        [Category(Categories.Data),
        DynamicBrowsableByProperty("ColumnType", BrowserColumnType.TextColumn, BrowserColumnType.DateColumn),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
        Editor("DevExpress.XtraEditors.Design.MaskDesigner, DevExpress.XtraEditors.v10.1.Design", typeof(UITypeEditor))]
        public MaskProperties Mask
        {
            get { return _Mask; }
        }

        #endregion

        #region LookupColumn
        [Category(Categories.Data),
        DynamicBrowsableByProperty("columnType", BrowserColumnType.LookupColumn),
        Editor(typeof(ProgNetDxComponents.Design.Editors.BrowserNameEditor), typeof(UITypeEditor))]
        public string BrowserName
        {
            get { return _BrowserName; }
            set
            {
                _BrowserName = value;
            }
        }

        #endregion
        
        #endregion

        #region ICloneable Members

        public object Clone()
        {
            BrowserColumn col = new BrowserColumn();
            col.Caption = Caption;
            col.Name = Name;
            col.Visible = Visible;
            col.SearchField = SearchField;
            col.FilterCondition = FilterCondition;
            col.Width = Width;
            col.ColumnType = ColumnType;
            col.ValueChecked = ValueChecked;
            col.ValueUnchecked = ValueUnchecked;
            col.AllowGrayed = AllowGrayed;
            col.Mask.Assign(Mask);
            col.BrowserName = BrowserName;
            col.Expression = Expression;
            col.DefaultValue = DefaultValue;
            return col;
        }

        #endregion

        public override string ToString()
        {
            return this.Name;
        }

       
    }

    [Editor(typeof(ProgNetDxComponents.Design.Editors.BrowserColumnCollectionEditor), typeof(UITypeEditor))]
    public class BrowserColumnCollection : NamedCollectionBase<BrowserColumn>
    {
        protected override bool AllowDuplicates
        {
            get
            {
                return true;
            }
        }
    }
}
